﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OneMake.Model;
using OneMake.DataAccess;

namespace OneMake.Winforms
{
    public partial class uc_report_kf_scope : UserControl, IBaseReportCtrl
    {
        private DataHelper dataHelper = new DataHelper();
        private ReportHelper rh = new ReportHelper();

        private int selShopId = 0;              //当前店铺
        private DateTime selBeginTime;          //开始时间
        private DateTime selEndTime;            //结束时间

        public uc_report_kf_scope()
        {
            InitializeComponent();
        }

        private void uc_report_kf_scope_Load(object sender, EventArgs e)
        {
            dtpBegin.Value = DateTime.Now.AddDays(-7);
            dtpEnd.Value = DateTime.Now;
            LoadShops();
        }
        
        private void LoadShops()
        {
            cbShops.DataSource = dataHelper.GetShops();
            cbShops.DisplayMember = "name";
            cbShops.ValueMember = "shopid";
            if (cbShops.Items.Count > 0)
                cbShops.SelectedIndex = 0;
        }

        public string ReportName
        {
            get { return "report_kf_scope"; }
        }

        public DockStyle Dock2 
        {
            get { return this.Dock; }
            set { this.Dock = value; }
        }

        public Control Parent2
        {
            get { return this.Parent; }
            set { this.Parent = value; }
        }

        public void LoadData() 
        { 
			lbProgress.Visible = true;
			panel1.Refresh();
			
			this.dgvReport.DataSource = null;
			this.dgvReport.Refresh();

            if (cbShops.SelectedValue != null)
                selShopId = System.Convert.ToInt32(cbShops.SelectedValue.ToString());
            else
                selShopId = 0;

            selBeginTime = dtpBegin.Value;
            selEndTime = dtpEnd.Value;

            Dictionary<string, string> attrMap = dataHelper.GetTableFieldMap(ReportName);

            dgvReport.Columns.Clear();
            foreach (KeyValuePair<string, string> item in attrMap)
            {
                DataGridViewColumn col = new DataGridViewTextBoxColumn();
                col.Name = item.Key;
                col.HeaderText = item.Value;
                col.DataPropertyName = item.Key;
                dgvReport.Columns.Add(col);
            }

            this.dgvReport.DataSource = rh.get_uc_report_kf_scopes(selShopId, selBeginTime, selEndTime);
            lbProgress.Visible = false;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Tools.ExportToExcel.ExportToExcel exp = new Tools.ExportToExcel.ExportToExcel();
            sfdExportToxcel.FileName = "导出文件1.xlsx";
            sfdExportToxcel.Filter = "Excel表格(*.xlsx)|*.xlsx|Todos los Archivos (*.*)|*.*";
            sfdExportToxcel.FilterIndex = 1;
            sfdExportToxcel.RestoreDirectory = true;

            if (sfdExportToxcel.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = sfdExportToxcel.FileName;
                try
                {
                    exp.dataGridView2Excel(this.dgvReport, path);
                    MessageBox.Show("导出完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            } 
        }

    }
}
